//
//  XFUserAccount.swift
//  mxfweibo
//
//  Created by apple1 on 15/12/19.
//  Copyright © 2015年 莫祥富. All rights reserved.
//

import UIKit

class XFUserAccount: NSObject ,NSCoding{
    
    //用户是否登录
    class var userLogin:Bool {
        return XFUserAccount.loadUserAccount() != nil
    }

    var access_token:String?
    
    var uid:String?
    
    var expires_in:NSTimeInterval = 0 {
        
        didSet{
            
            expires_Date = NSDate(timeIntervalSinceNow: expires_in)
        }
    }
   
    var expires_Date : NSDate?
    
    var screen_name : String?
    
    var avatar_large : String?
    
    
    init(dict:[String:AnyObject]){
        
        super.init()
        
        setValuesForKeysWithDictionary(dict)
    }
    
    //当字典的key在模型里面找不到属性的时候调用
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
    override  var description:String{
        get {
            return "access_token: \(access_token), uid: \(uid), expires_in: \(expires_in), expires_Date: \(expires_Date)"
        }
    }
    
    // MARK: - 归档和解档
    
    ///归档
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeDouble(expires_in, forKey: "expires_in")
        aCoder.encodeObject(expires_Date, forKey: "expires_Date")
        aCoder.encodeObject(screen_name, forKey: "screen_name")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
    }
    
    //解档
    required init?(coder aDecoder: NSCoder) {
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        uid = aDecoder.decodeObjectForKey("uid") as? String
        expires_in = aDecoder.decodeDoubleForKey("expires_in")
        expires_Date = aDecoder.decodeObjectForKey("expires_Date") as? NSDate
        screen_name = aDecoder.decodeObjectForKey("screen_name") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
    }
    
    //获取沙盒路径 拼接文件名
    static let path = (NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory,NSSearchPathDomainMask.UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("userAccount.plist")
    
    //保存账户信息
    func saveUserAccount(){
        
        NSKeyedArchiver.archiveRootObject(self, toFile: XFUserAccount.path)
    }
    
    static var userAccount : XFUserAccount?
    
    
    //加载帐号信息
    class func loadUserAccount() ->XFUserAccount? {
        if userAccount == nil {
            userAccount = NSKeyedUnarchiver.unarchiveObjectWithFile(path) as? XFUserAccount
        }
        
        if userAccount != nil && userAccount?.expires_Date?.compare(NSDate()) == NSComparisonResult.OrderedDescending {
            print("加载到可用帐号")
            return userAccount
        }
        return nil
    }

    //加载用户信息
    func loadUserInfo(finished:(error:NSError?) -> ()){
        
        XFNetworkTool.sharedInstance.loadUserInfo { (result, error) -> () in
            
            if error != nil || result == nil {
                print("数据加载有误:\(error)")
                finished(error: error)
                return
            }
            
            self.screen_name = result!["screen_name"] as? String
            self.avatar_large = result!["avatar_large"] as? String
            
            self.saveUserAccount()
            XFUserAccount.userAccount = self
            finished(error: nil)
        }
    }
    
    
}
